<template>
  <div class="page-container">
    <div v-if="showFlag == 1" class="content-wrapper">
      <el-card class="table-card" shadow="never">
        <template #header>
          <div class="card-header">
            <h2 class="card-title">
              <i class="el-icon-user-solid"></i>
              用户权限管理
            </h2>
            <el-button
                :icon="Refresh"
                circle
                class="refresh-btn"
                @click="updateUserList"
            />
          </div>
        </template>

        <div class="table-container">
          <el-table
              v-loading="loading"
              :data="userTableData"
              :fit="true"
              class="elegant-table"
              empty-text="暂无用户数据"
          >
            <el-table-column
                align="center"
                label="ID"
                prop="id"
                width="100"
            />
            <el-table-column
                align="center"
                label="用户名"
                prop="username"
                width="180"
            />
            <el-table-column
                align="center"
                label="昵称"
                min-width="200"
                prop="nickname"
            />
            <el-table-column
                align="center"
                fixed="right"
                label="操作"
                width="150"
            >
              <template #default="scope">
                <el-button
                    :disabled="!isAdmin"
                    :icon="Setting"
                    class="auth-btn"
                    size="small"
                    @click="handleAuthClick(scope.row)"
                >
                  权限设置
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </div>

    <div v-if="showFlag == 2" class="system-config-container">
      <el-card class="config-card" shadow="never">
        <template #header>
          <div class="card-header">
            <div class="card-header-left">
              <el-button
                  :icon="ArrowLeft"
                  class="back-btn"
                  text
                  @click="handleBackupClick"
              >
                返回
              </el-button>
              <div class="user-info">
                <span class="header-title">用户存储配置</span>
                <el-tag class="user-tag" effect="plain" type="info">
                  <i class="el-icon-user"></i>
                  {{ selectedUser.username }} ({{ selectedUser.nickname }})
                </el-tag>
              </div>
            </div>
          </div>
        </template>

        <el-form
            :model="configForm"
            class="config-form"
            label-position="left"
            label-width="180px"
        >
          <!-- 存储权限设置 -->
          <div class="section-header">
            <i class="el-icon-folder"></i>
            <span>存储权限设置</span>
          </div>

          <el-form-item label="文件存储类型">
            <el-switch
                v-model="configForm.fileStorageTypeFlag"
                active-text="minio存储"
                inactive-text="本地存储"
                inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949; margin-left: 10px"
            />
            <div class="form-tip">
              {{
                configForm.fileStorageTypeFlag ?
                    '文件将存储在MinIO对象存储中' :
                    '文件将存储在服务器本地文件系统中'
              }}
            </div>
          </el-form-item>

          <el-form-item
              v-if="!configForm.fileStorageTypeFlag"
              label="本地存储路径"
          >
            <el-input
                v-model="configForm.localFileStoragePath"
                clearable
                placeholder="请输入本地存储路径"
                style="width: 400px"
            >
              <template #prepend>
                <el-button :icon="FolderOpened"/>
              </template>
            </el-input>
            <div class="form-tip">
              请确保服务器对该路径有读写权限
            </div>
          </el-form-item>
        </el-form>

        <div class="dialog-footer">
          <el-button
              :icon="Close"
              class="footer-btn"
              @click="handleBackupClick"
          >
            取消
          </el-button>
          <el-button
              :icon="Check"
              :loading="saving"
              class="footer-btn"
              type="primary"
              @click="saveConfig"
          >
            保存配置
          </el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, ref} from "vue"
import {
  Refresh,
  Setting,
  ArrowLeft,
  FolderOpened,
  Check,
  Close
} from '@element-plus/icons-vue'
import {getAllUserList} from "@/api/system/sysUser"
import message from '@/utils/ElmessasgeUtils'
import {getUserStorageConfig, saveUserStorageConfig} from "@/api/documentManage/config";
import {getUserInfo} from "@/utils/system";
import type {SysUserInfo} from "@/type/systemConfig/sysUserInfo";
import {useMainStore} from "@/main";

const isAdmin = getUserInfo().roleId === 1
const showFlag = ref(1)
const userTableData = ref<SysUserInfo[]>()
const loading = ref(false)
const saving = ref(false)

function updateUserList() {
  loading.value = true
  getAllUserList().then(res => {
    if (res.data.code === 200) {
      userTableData.value = res.data.data
    } else {
      message.message('获取用户列表失败', 'error')
    }
  }).finally(() => {
    loading.value = false
  })
}

function loadStorageConfig(userId: string) {
  getUserStorageConfig(userId).then((res: any) => {
    if (res.data.code === 200) {
      configForm.value.fileStorageTypeFlag = res.data.data.storageType === 0 ? true : false
      configForm.value.localFileStoragePath = res.data.data.storagePath
    } else {
      console.log("发生了错误")
    }
  })
}

const selectedUser = ref<SysUserInfo>()

function handleAuthClick(row: any) {
  showFlag.value = 2;
  selectedUser.value = row
  loadStorageConfig(row.userId)
}

const configForm = ref({
  fileStorageTypeFlag: true,
  localFileStoragePath: '/home',
})

function saveConfig() {
  saving.value = true

  let data = {
    userId: selectedUser.value?.userId,
    storageType: configForm.value.fileStorageTypeFlag ? 0 : 1,
    storagePath: configForm.value.localFileStoragePath
  }

  saveUserStorageConfig(data).then((res: any) => {
    if (res.data.code == 200) {
      // 刷新Pinia 配置
      useMainStore().fileStorageType = configForm.value.fileStorageTypeFlag
      useMainStore().localFileStoragePath = configForm.value.localFileStoragePath

      message.message('保存成功')
    } else {
      message.message('保存失败', 'error')
    }
  }).finally(() => {
    saving.value = false
  })
}

const handleBackupClick = () => {
  showFlag.value = 1
}

onMounted(() => {
  updateUserList()
})
</script>

<style lang="scss" scoped>
.page-container {
  width: 100%;
  min-height: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7ed 100%);
  padding: 20px;
  box-sizing: border-box;
}

.content-wrapper {
  width: 100%;
  max-width: 800px; // 减少最大宽度以适应列数
  margin: 0 auto;
}

.table-container {
  display: flex;
  justify-content: center;
}

.elegant-table {
  width: auto; // 改为自动宽度
  min-width: 600px; // 设置最小宽度
  margin: 0 auto;

  // 其他表格样式保持不变
  :deep(.el-table__header) th {
    font-weight: 600;
    background: linear-gradient(to bottom, #f8fafd, #f0f5ff);
    color: #409EFF;
    height: 50px;
  }

  :deep(.el-table__row) {
    transition: all 0.3s;

    &:hover td {
      background-color: #f5f9ff;
    }

    td {
      border-bottom: 1px solid #f0f4f8;
      padding: 12px 0;
      transition: all 0.3s;
    }
  }
}

.table-card {
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #eee;

    .card-title {
      margin: 0;
      font-size: 1.5rem;
      font-weight: 600;
      color: #2c3e50;
      display: flex;
      align-items: center;

      i {
        margin-right: 10px;
        color: #409EFF;
      }
    }

    .refresh-btn {
      background: #f5f7fa;
      border: none;
      color: #409EFF;
      transition: all 0.3s;

      &:hover {
        background: #409EFF;
        color: white;
        transform: rotate(90deg);
      }
    }
  }
}

.elegant-table {
  --el-table-border-color: #ebeef5;
  --el-table-header-bg-color: #f8fafd;
  --el-table-row-hover-bg-color: #f5f9ff;
  --el-table-text-color: #606266;
  --el-table-header-text-color: #303133;
  border-radius: 8px;
  overflow: hidden;

  :deep(.el-table__header) th {
    font-weight: 600;
    background: linear-gradient(to bottom, #f8fafd, #f0f5ff);
    color: #409EFF;
    height: 50px;
  }

  :deep(.el-table__row) {
    transition: all 0.3s;

    &:hover td {
      background-color: #f5f9ff;
    }

    td {
      border-bottom: 1px solid #f0f4f8;
      padding: 12px 0;
      transition: all 0.3s;
    }
  }
}

.auth-btn {
  background: linear-gradient(to right, #409EFF, #64b5ff);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 12px;
  transition: all 0.3s;

  &:hover {
    background: linear-gradient(to right, #64b5ff, #409EFF);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
    transform: translateY(-2px);
  }

  &:disabled {
    background: #c0c4cc;
    cursor: not-allowed;

    &:hover {
      box-shadow: none;
      transform: none;
    }
  }
}

.system-config-container {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

.config-card {
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

  .card-header {
    padding: 16px 20px;
    border-bottom: 1px solid #eee;

    .card-header-left {
      display: flex;
      align-items: center;
      gap: 15px;

      .back-btn {
        color: #409EFF;
        padding: 8px 12px;
        border-radius: 6px;
        transition: all 0.3s;

        &:hover {
          background: #f5f9ff;
          color: #64b5ff;
        }
      }

      .user-info {
        display: flex;
        flex-direction: column;

        .header-title {
          font-size: 1.4rem;
          font-weight: 600;
          color: #2c3e50;
          margin-bottom: 5px;
        }

        .user-tag {
          align-self: flex-start;
          background: #f0f5ff;
          color: #409EFF;
          border: none;
          border-radius: 4px;
        }
      }
    }
  }
}

.config-form {
  padding: 20px;

  .section-header {
    display: flex;
    align-items: center;
    margin-bottom: 25px;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
    color: #409EFF;
    font-size: 1.1rem;
    font-weight: 600;

    i {
      margin-right: 10px;
      font-size: 1.2rem;
    }
  }

  :deep(.el-form-item) {
    margin-bottom: 24px;
    padding: 15px;
    border-radius: 8px;
    transition: all 0.3s;

    &:hover {
      background: #f9fbfd;
    }

    .el-form-item__label {
      font-weight: 500;
      color: #606266;
      padding-right: 20px;
    }
  }

  .form-tip {
    margin-top: 8px;
    font-size: 0.85rem;
    color: #909399;
    line-height: 1.4;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #eee;
  margin-top: 20px;

  .footer-btn {
    border-radius: 6px;
    padding: 10px 20px;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .page-container {
    padding: 10px;
  }

  .config-form {
    padding: 10px;

    :deep(.el-form-item__label) {
      width: 100% !important;
      margin-bottom: 8px;
    }
  }

  .dialog-footer {
    flex-direction: column;

    .footer-btn {
      width: 100%;
    }
  }
}
</style>